/**
  ******************************************************************************
  * @file           : time_count_task.cpp
  * @author         : Tessia
  * @brief          : None
  * @attention      : None
  * @date           : 2024/3/24
  ******************************************************************************
  */



#include "time_count_task.h"


extern uint8_t enemy_aim_enable[12];
extern judge_info_0 judgeInfo0;
extern judge_info_1 judgeInfo1;
extern uint8_t outpost_be_destroyed;
extern uint8_t time_short;
extern uint32_t be_hit_flag;
extern enemy_coo enemyCoo;
extern uint8_t armor_number_no_zero;
extern uint16_t attack_engineer_time;
extern sentry_state normal_state;

float out_patrol_zone_time = 90;
int32_t out_patrol_zone_time_ms = 90000; //单位ms
uint32_t pursuit_time_ms = 0;
float pursuit_time = 0;

void time_count_task(void *argument){
    static uint32_t enemy_hero_invincibility_time = 0;
    static uint32_t enemy_engineer_invincibility_time = 0;
    static uint32_t enemy_infantry_3_invincibility_time = 0;
    static uint32_t enemy_infantry_4_invincibility_time = 0;
    static uint32_t enemy_infantry_5_invincibility_time = 0;
    static uint32_t enemy_sentry_invincibility_time = 0;
    static uint32_t be_hit_delay_time = 0;
    uint32_t be_hit_flag_last = 0;
    uint16_t stage_remain_time_last = judgeInfo0.stage_remain_time;
    sentry_state normal_state_last_time_cnt_task = standby;
    for(;;){
        enemy_hero_invincibility_time += enemy_aim_enable[hero] == 0 ? 1 : 0;
        if(enemy_hero_invincibility_time > 10000){
            enemy_aim_enable[hero] = 1;
            enemy_hero_invincibility_time = 0;
        }

        enemy_engineer_invincibility_time += (enemy_aim_enable[engineer] == 0 && judgeInfo0.game_progress == during_the_match && judgeInfo0.stage_remain_time < attack_engineer_time
                && (enemyCoo.x < 15.96 || enemyCoo.y < 3.25)) ? 1 : 0;
        enemy_aim_enable[engineer] = ((enemyCoo.x >= 15.96 && enemyCoo.y >= 3.25) && armor_number_no_zero == engineer) ? 0 : enemy_aim_enable[engineer];
        if(enemy_engineer_invincibility_time > 10000){
            enemy_aim_enable[engineer] = 1;
            enemy_engineer_invincibility_time = 0;
        }

        enemy_infantry_3_invincibility_time += enemy_aim_enable[infantry_3] == 0 ? 1 : 0;
        if(enemy_infantry_3_invincibility_time > 10000){
            enemy_aim_enable[infantry_3] = 1;
            enemy_infantry_3_invincibility_time = 0;
        }
        enemy_infantry_4_invincibility_time += enemy_aim_enable[infantry_4] == 0 ? 1 : 0;
        if(enemy_infantry_4_invincibility_time > 10000){
            enemy_aim_enable[infantry_4] = 1;
            enemy_infantry_4_invincibility_time = 0;
        }

        enemy_infantry_5_invincibility_time += enemy_aim_enable[infantry_5] == 0 ? 1 : 0;
        if(enemy_infantry_5_invincibility_time > 10000){
            enemy_aim_enable[infantry_5] = 1;
            enemy_infantry_5_invincibility_time = 0;
        }

        pursuit_time_ms = ((normal_state == pursuit && normal_state_last_time_cnt_task != pursuit) || (normal_state != pursuit && normal_state_last_time_cnt_task == pursuit)) ? 0 : pursuit_time_ms;
        pursuit_time_ms += normal_state == pursuit ? 1 : 0;
        pursuit_time = (float)pursuit_time_ms / 1000.0f;

#if MATCH_MODE
        if(outpost_be_destroyed == 1 && judgeInfo0.game_progress == during_the_match){
#elif CONSTRAINED_DEFENCE_DEBUG
        if(judgeInfo0.game_progress == during_the_match){
#else
        if(outpost_be_destroyed == 1 && judgeInfo0.game_progress == during_the_match){
#endif
            if(judgeInfo0.stage_remain_time > TIME_CONVERSION_NODE){
                out_patrol_zone_time_ms = judgeInfo0.is_occupy_patrol_zone == 1 ? 90000 : out_patrol_zone_time_ms;
                if(judgeInfo0.stage_remain_time - out_patrol_zone_time < TIME_CONVERSION_NODE - 5){
                    out_patrol_zone_time_ms = (judgeInfo0.stage_remain_time - TIME_CONVERSION_NODE + 30) * 1000.f;
                }
            }else{
                out_patrol_zone_time_ms = judgeInfo0.is_occupy_patrol_zone == 1 ? 30000 : out_patrol_zone_time_ms;
            }
            out_patrol_zone_time_ms -= judgeInfo0.is_occupy_patrol_zone == 0 ? 1 : 0;
        }
        out_patrol_zone_time = (float)out_patrol_zone_time_ms / 1000.f;
        time_short = out_patrol_zone_time <= 32 ? 1 : 0;

        be_hit_delay_time += be_hit_flag > 0 ? 1 : 0;
        if(be_hit_flag > be_hit_flag_last){
            be_hit_delay_time = 0;
        }
        if(be_hit_delay_time > 2000){
            be_hit_flag = 0;
            be_hit_delay_time = 0;
        }

        stage_remain_time_last = judgeInfo0.stage_remain_time;
        be_hit_flag_last = be_hit_flag;
        normal_state_last_time_cnt_task = normal_state;
        osDelay(1);
    }
}